<?php

namespace plugin\admin\app\model\withdraw;


use app\common\lib\Common;
use app\exception\IndexException;
use plugin\admin\app\model\Base;

class UserWithdrawModel extends Base
{
    protected $table = 'video_user_withdraw';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected static $instance;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function saveData($param)
    {
        if(!empty($param['id'])){
            $info = $this->find($param['id']);
            if($info==false){
                throw new IndexException(4020,'数据不存在');
            }
            $data = [];
            !empty($param['manage_remark']) && $data['manage_remark'] = $param['manage_remark'];
            !empty($param['givemoney_time']) && $data['givemoney_time'] = $param['givemoney_time'];

            !empty($param['status']) && $data['status'] = $param['status'];
            !empty($param['isdel']) && $data['isdel'] = $param['isdel'];

            return $this->where(['id'=>$param['id']])->update($data);
        }else{
            $data = [];
            $maxId =  $this->where([['withdrawsn','>',0]])->max('withdrawsn');
            $data['withdrawsn'] = Common::getSnCode($maxId,'40',4,2);

            $data['user_sn'] = $param['user_sn']??request()->curUserId;
            $data['amount'] = $param['amount']??'0';
            $data['readly_amount'] = $param['readly_amount']??'0';
            $data['can_money'] = $param['can_money']??'0';
            $data['mobile'] = $param['mobile']??'';
            $data['truename'] = $param['truename']??'';
            $data['account'] = $param['account']??'';
            $data['idcard'] = $param['idcard']??'';
            $data['bankname'] = $param['bankname']??'';
            $data['type'] = $param['type']??'1';
            $data['total_settle_amount'] = $param['total_settle_amount']??'0';
            $data['total_withdraw_amount'] = $param['total_withdraw_amount']??'0';

            $id = $this->insertGetId($data);

            return $id;
        }
    }
}
